package com.woniu.mealsService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.annotation.RequireRole;
import com.woniu.exception.meals.MealsException;
import com.woniu.mealsService.model.Address;
import com.woniu.mealsService.service.AddressService;
import com.woniu.utils.EnumException;
import com.woniu.utils.JwtUtil;
import com.woniu.utils.Result;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.List;

/**
 * <p>
 * 地址表 前端控制器
 * </p>
 *
 * @author jiang
 * @since 2024年12月09日
 */
@RestController
@RequestMapping("/address")
@Api(tags = "地址信息接口")

public class AddressController {

    @Resource
    AddressService addressService;

    @GetMapping("/insertAddress")
    @ApiOperation("新增地址")
    @RequireRole("学生")
    @ApiImplicitParams({@ApiImplicitParam(name = "addressName", value = "收货地址"),
            @ApiImplicitParam(name = "consignee", value = "收货人"),
            @ApiImplicitParam(name = "consigneePhone", value = "收货人手机号")})

    public Result insertAddress(@NotNull(message = "收货地址不能为空")@RequestParam String addressName, @NotNull(message = "收货人不能为空")@RequestParam  String consignee, @NotNull(message = "手机号不能为空")
    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")@RequestParam  String consigneePhone, @RequestHeader("token") String token) throws Exception {
        Claims claims = JwtUtil.parseToken(token);
        String operatorId = claims.get("userId").toString();
        addressService.insertAddress(addressName, operatorId, consignee, consigneePhone);
        return Result.getResult().setCode(200).setMsg("新增地址成功");
    }

    @GetMapping("/updateAddress")
    @RequireRole("学生")
    @ApiOperation("修改地址")
    @ApiImplicitParams({@ApiImplicitParam(name = "newAddressName", value = "新的详细地址"), @ApiImplicitParam(name = "addressId", value = "要修改的地址id")})
    public Result updateAddress(@NotNull(message = "新收货地址不能为空")@RequestParam String newAddressName, @Min(value = 1, message = "地址id必须为正整数") @Max(value = 999, message = "地址id不能超过3位")@RequestParam Integer addressId, @NotNull(message = "收货人不能为空")@RequestParam  String consignee, @NotNull(message = "手机号不能为空")
    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")@RequestParam  String consigneePhone) throws Exception {
        addressService.updateAddress(newAddressName, addressId,consignee,consigneePhone);
        return Result.getResult().setCode(200).setMsg("修改地址成功");
    }

    @GetMapping("/deleteAddress")
    @ApiOperation("删除地址")
    @RequireRole("学生")
    @ApiImplicitParam(name = "addressId", value = "要删除的地址id")
    public Result deleteAddress(@Min(value = 1, message = "地址id必须为正整数") @Max(value = 999, message = "地址id不能超过3位")@RequestParam Integer addressId) throws Exception {
        addressService.removeById(addressId);
        return Result.getResult().setCode(200).setMsg("删除地址成功");
    }

    @GetMapping("/selectAddress")
    @ApiOperation("查询个人所有地址")
    @RequireRole("学生")
    public Result<List<Address>> selectAddress(@Min(value = 1, message = "用户账号id必须为正整数") @Max(value = 999, message = "用户账号id不能超过3位")@RequestParam Integer userId) throws Exception {
        List<Address> addressList = addressService.list(new QueryWrapper<Address>().eq("user_id", userId));
        return Result.getListResult(Address.class).setMsg("查询个人地址成功").setCode(200).setData(addressList);
    }

    @GetMapping("/setDefaultAddress")
    @ApiOperation("设置默认地址")
    @ApiImplicitParam(name = "addressId", value = "要设置默认地址id")
    @RequireRole("学生")
    public Result setDefaultAddress(@Min(value = 1, message = "地址id必须为正整数") @Max(value = 999, message = "地址id不能超过3位")@RequestParam Integer addressId,@Min(value = 1, message = "用户账号id必须为正整数") @Max(value = 999, message = "用户账号id不能超过3位")@RequestParam  Integer userId) throws Exception {
        addressService.setDefaultAddress(addressId, userId);
        return Result.getResult().setCode(200).setMsg("设置默认地址成功");
    }

    @GetMapping("/selectAddressById")
    @ApiOperation("通过地址id查询地址")
    public Result<Address> selectAddressById(@Min(value = 1, message = "地址id必须为正整数") @Max(value = 999, message = "地址id不能超过3位")@RequestParam Long addressId) throws Exception {
        Address address = addressService.getOne(new QueryWrapper<Address>().eq("address_id", addressId));
        if (address == null) {
            throw new MealsException(EnumException.ADDRESS_NULL_EXCEPTION);
        }
        return Result.getResult(Address.class).setMsg("查询地址成功").setCode(200).setData(address);
    }

}

